﻿using System;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.CM
{
    /// <summary>
    /// Linkman 的摘要说明。
    /// </summary>
    public partial class LinkmanPage : Page
    {
        private int clientid;
        private string from = "";
        private int linkmanid;

        private void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                clientid = ((Request.QueryString["ClientID"] == null) || (Request.QueryString["ClientID"].Trim() == "")) ? 0 : Int32.Parse(Request.QueryString["ClientID"]);
                linkmanid = ((Request.QueryString["LinkmanID"] == null) || (Request.QueryString["LinkmanID"].Trim() == "")) ? 0 : Int32.Parse(Request.QueryString["LinkmanID"]);
                from = (Request.QueryString["From"] == null) ? "" : Request.QueryString["From"];
                ViewState["LinkmanID"] = linkmanid.ToString();
                BindData();

                if (Session["cm_permission"].ToString() == "leader")
                    btn_OK.Visible = false;
            }
            else
            {
                linkmanid = Int32.Parse(ViewState["LinkmanID"].ToString());
                clientid = Int32.Parse(ddl_ClientName.SelectedItem.Value);
            }
            if (linkmanid == 0)
            {
                btn_OK.Text = "添加";
                btn_Del.Visible = false;
            }
            else
            {
                btn_OK.Text = "修改";
                btn_Del.Visible = true;
            }

            btn_Del.Attributes["onclick"] = "return confirm('确定要删除吗?')";
        }

        private void BindData()
        {
            var cm = new Components.CM();
            Components.Linkman linkman;

            if (linkmanid != 0)
            {
                linkman = cm.GetLinkmanStructByID(linkmanid.ToString());
                clientid = linkman.ClientID;
            }

            //dr = cm.GetClientInfo(clientid);
            SqlDataReader dr = cm.GetMyClients(SiteContext.Current.UserID);
            ClientInfo client = cm.GetClientAllInfo(clientid);
            //绑定客户下拉列表
            ddl_ClientName.DataSource = dr;
            ddl_ClientName.DataTextField = "Name";
            ddl_ClientName.DataValueField = "id";
            ddl_ClientName.DataBind();

            dr.Close();
            if ((clientid == 0) && (from) != "ClientList")
                ddl_ClientName.Items.Insert(0, new ListItem("当前客户", "0"));
            else
            {
                //使传入的clientid处于选中状态
                foreach (ListItem lt in ddl_ClientName.Items)
                {
                    if (lt.Value == clientid.ToString())
                    {
                        lt.Selected = true;
                    }
                }
            }


            dr.Close();
            if (linkmanid == client.ChiefLinkmanID) cb_chief.Checked = true;
            //填充联系人信息
            dr = cm.GetLinkmanByID(linkmanid.ToString());
            while (dr.Read())
            {
                tb_Name.Text = dr["Name"].ToString();
                tb_Mobile.Text = dr["Mobile"].ToString();
                tb_Telephone.Text = dr["Telephone"].ToString();
                tb_Position.Text = dr["Position"].ToString();
                tb_Email.Text = dr["Email"].ToString();
                //填充性别
                if (Convert.ToBoolean(dr["gender"]))
                {
                    ddl_Gender.Items[0].Selected = true;
                    ddl_Gender.Items[1].Selected = false;
                }
                else
                {
                    ddl_Gender.Items[0].Selected = false;
                    ddl_Gender.Items[1].Selected = true;
                }

                tb_Description.Text = dr["description"].ToString();
                tb_Address.Text = dr["address"].ToString();
                tb_Family.Text = dr["family"].ToString();
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            var cm = new Components.CM();
            var linkman = new Components.Linkman();
            linkman.ClientID = clientid;
            linkman.Address = tb_Address.Text;
            linkman.Description = tb_Description.Text;
            linkman.Email = tb_Email.Text;
            linkman.Family = tb_Family.Text;
            linkman.Gender = (ddl_Gender.SelectedItem.Value == "1") ? true : false;
            linkman.Mobile = tb_Mobile.Text;
            linkman.Name = tb_Name.Text;
            linkman.Position = tb_Position.Text;
            linkman.Telephone = tb_Telephone.Text;
            linkman.ID = linkmanid;
            try
            {
                if (linkmanid != 0) //修改信息
                {
                    if (cb_chief.Checked)
                    {
                        ClientInfo clientinfo = cm.GetClientAllInfo(clientid);
                        clientinfo.ChiefLinkmanID = linkman.ID;
                        cm.UpdateClient(clientinfo);
                    }
                    else
                    {
                        ClientInfo clientinfo = cm.GetClientAllInfo(clientid);
                        clientinfo.ChiefLinkmanID = 0;
                        cm.UpdateClient(clientinfo);
                    }
                    cm.UpdateLinkman(linkman);
                }
                else //添加联系人
                {
                    //如果选择了客户则添加联系人并且修改该客户的主要联系人
                    if (clientid != 0)
                    {
                        linkman.ID = cm.AddLinkman(linkman);
                        if (cb_chief.Checked)
                        {
                            ClientInfo clientinfo = cm.GetClientAllInfo(clientid);
                            clientinfo.ChiefLinkmanID = linkman.ID;
                            cm.UpdateClient(clientinfo);
                        }

                        Response.Write("<script>if(opener!=null) opener.location.href=opener.location.href;</script>");
                    }
                    else //如果没有选择客户则联系人保存为临时状态即id=0
                    {
                        string strlinkmanid = cm.AddLinkman(linkman).ToString();
                        Session["tmplinkmanid"] += strlinkmanid + ",";
                        if (cb_chief.Checked) Session["tmpchief"] = strlinkmanid;
                        Response.Write("<script>if(opener!=null) opener.location.href=opener.location.href;</script>");
                    }
                    Response.Write("<script>window.close();</script>");
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex.Message);
                Server.Transfer("../Error.aspx");
            }
        }

        private void btn_Del_Click(object sender, EventArgs e)
        {
            var cm = new Components.CM();
            var linkman = new Components.Linkman();
            linkman.ID = linkmanid;
            if (cm.DelLinkman(linkman))
            {
                Response.Write("<script>if(opener!=null) opener.location.href=opener.location.href;alert('删除成功!');window.close();</script>");
            }
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.btn_OK.Click += new System.EventHandler(this.btn_OK_Click);
            this.btn_Del.Click += new System.EventHandler(this.btn_Del_Click);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion
    }
}